<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/24 0024
 * Time: 16:02
 */

class Zlhl {

    private static $client_id = 25;
    private static $secret = 'a2c7b4002fa9bf48f6d74a3e293334cc';
    public static $_service_url = 'http://member.stourweb.cn';
    public static $_bind_supplier_path = '/api/zlhl/get_member_info';
    public static $_app_install_url = 'http://enterprise.zhilvhulian.com/app/front/add_enterprise_app';

    public static function create_sign($url, array $query_strings)
    {
        $seed = $url;
        foreach ($query_strings as $key => $query_string) {
            if ($key != "sign") {
                $seed .= $query_string;
            }
        }
        return md5($seed . self::$secret);
    }

    /**
     * @function 安装应用
     * @param $memberid
     * @param $app_name
     * @param $app_url
     * @param null $enterprise_id
     * @param null $app_icon
     */
    public static function install_app($identifier,$memberid,$app_name,$app_url,$enterprise_id=null,$app_icon=null,$has_msg=0)
    {
       $params = array('identifier'=>$identifier,'memberid'=>$memberid,'name'=>$app_name,'url'=>$app_url,$enterprise_id=>$enterprise_id,'icon'=>$app_icon,'has_msg'=>$has_msg);
       if(!isset($params['belongs_to']))
       {
           $params['belongs_to'] = 'supplier';
       }
       $result = self::request_api(self::$_app_install_url,$params);
       return $result;
    }

    public static function http($url, $postfields = '', $method = 'GET')
    {
        if (strpos($url, 'http') === false) 
		{
            return;
        }
        $ci = curl_init();
        $user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
        curl_setopt($ci,CURLOPT_FOLLOWLOCATION,1);// 当遇到301/302 直接抓取页面
        curl_setopt($ci, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        if ($method == 'POST') {
            curl_setopt($ci, CURLOPT_POST, true);
            if ($postfields != '') {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        $response = curl_exec($ci);
		
		
        curl_close($ci);
        return $response;
    }

    public static function request_api($url,$params)
    {

        $params = is_array($params)?$params:array();
        $params["client_id"] =self::$client_id;
        $params["sign"] = self::create_sign($url, $params);

        $request_url = $url . '?' . http_build_query($params);
        $result = json_decode(self::http($request_url),true);
        return $result;

    }

    public static function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
    {
        $ckey_length = 4;
        $key = md5($key ? $key : 'stourweb');
        $keya = md5(substr($key, 0, 16));
        $keyb = md5(substr($key, 16, 16));
        $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
        $cryptkey = $keya . md5($keya . $keyc);
        $key_length = strlen($cryptkey);
        $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
        $string_length = strlen($string);
        $result = '';
        $box = range(0, 255);
        $rndkey = array();
        for ($i = 0; $i <= 255; $i++)
        {
            $rndkey[$i] = ord($cryptkey[$i % $key_length]);
        }
        for ($j = $i = 0; $i < 256; $i++)
        {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        for ($a = $j = $i = 0; $i < $string_length; $i++)
        {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }
        if ($operation == 'DECODE')
        {

            if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16))
            {
                return substr($result, 26);
            }
            else
            {
                return '';
            }
        }
        else
        {
            return $keyc . str_replace('=', '', base64_encode($result));
        }
    }




}